/**
 * $Id$
 * Copyright 2011-2014 Oak Pacific Interactive. All rights reserved.
 */
package com.malphi.framework.memcache.manage;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
import com.malphi.framework.core.xmlconfig.ConfigFactory;
import com.malphi.framework.core.xmlconfig.impl.MemCacheConfig;
import com.malphi.framework.core.xmlconfig.impl.MemCachePoolConfig;


/**
 * @author <a href="mailto:ruofei.ma@renren-inc.com">ma ruofei</a>
 * @version 1.0 2011-11-21 上午11:20:04
 * @since 1.0
 */
public class MemCacheManager {
    private static MemCacheConfig cacheConfig = (MemCacheConfig) ConfigFactory.getConfigMap().get(MemCacheConfig.class);
    private static MemCachePoolConfig poolConfig = cacheConfig.getPoolConfig();
    private MemCacheManager(){}
    private static MemCachedClient client;
    static{
        initPoolConfig();
        client = new MemCachedClient(poolConfig.getPoolName());
    }

    public static MemCachedClient getCacheClient(){
        return client;
    }
    public static MemCacheConfig getConfig(){
        return cacheConfig;
    }
    /**
     *  init pool
     */
    private static void initPoolConfig() {
        SockIOPool pool = SockIOPool.getInstance(poolConfig.getPoolName());
        if(pool.isInitialized()) return;
        String[] servers = poolConfig.getServers();
        pool.setServers(servers);
        pool.setHashingAlg(SockIOPool.CONSISTENT_HASH);
        pool.setInitConn(servers.length);
        pool.setMaxConn(servers.length * 600);
        pool.setMaintSleep(1000L);
        pool.setMaxIdle(1800000L);//最长空闲时间30分
        pool.setNagle(false);
        pool.setSocketTO(500);//设置超时时间  500ms
        pool.setAliveCheck(true);
        pool.initialize();
    }
    
    
    
}
